#include <iostream>
#include <cstdlib>
#include <list>
#include <vector>
#include <iterator>
using namespace std;

vector<int> answer;
vector<int> bak, seq;
vector<int>::iterator pos, npos;

bool check(int n, int step)	//进行模拟测试当前数能否成功 
{
    int i, j, p;
    pos = seq.begin();    
    while (seq.size()>1)
    {
        if (*pos == 2) return false;
        npos = seq.erase(pos);
        if (npos == seq.end()) npos = seq.begin();
        for (i=1; i<step; ++i) 
        {
            npos++;
            if (npos == seq.end()) npos = seq.begin();
        }    
        pos = npos;
    }    
    return true;
}    

int getans(int n)
{   
    int i, maxv;
    bak.clear();
    bak.reserve(n);
    for (i=1; i<=n; ++i) bak.push_back(i);	//备份一个list序列 
    
    maxv = 1;
    for (i=2;  ; ++i)	//直到找到才退出 
    {
        seq = bak;
        if (check(n, i)) 
        {
            maxv = i;
            break;
        }    
    }    
    return maxv;
}    

int main()
{
    int i;     
    answer.resize(3);
    for (i=3; i<150; ++i) 
    	answer.push_back(getans(i));	//把答案先预先算出来 
    	
   	while (1)
   	{
   	    cin >> i;
   	    if (!i) break;
   	    cout << answer[i] << endl;
   	}    
                
 //   system("pause"); 
    return 0;    
}    